package com.zhuhjay.common.core.config;

import cn.hutool.core.util.StrUtil;
import com.gitee.zhuhjay.xunfei.sdk.AppClientFactory;
import com.gitee.zhuhjay.xunfei.sdk.model.ApiAuth;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author ZhuHJay
 * @date 2023/11/12 23:01
 */
@Configuration
public class XunFeiAiConfig {

    @Bean
    @ConfigurationProperties(prefix = "generic-boot.xun-fei")
    public ApiAuth apiAuth() {
        return ApiAuth.builder().build();
    }

    @Bean
    public AppClientFactory appClientFactory(ApiAuth apiAuth) {
        // 如果没有配置, 那么就直接返回空(防止没配置时启动失败)
        if (StrUtil.isBlank(apiAuth.getAppId()) ||
                StrUtil.isBlank(apiAuth.getApiKey()) ||
                StrUtil.isBlank(apiAuth.getApiSecret())) {
            return null;
        }
        // 如果配置的是占位符, 那么也直接返回空(防止没配置时启动失败)
        if (apiAuth.getAppId().startsWith("${") ||
                apiAuth.getApiKey().startsWith("${") ||
                apiAuth.getApiSecret().startsWith("${")) {
            return null;
        }
        return AppClientFactory.create(apiAuth);
    }

}
